'''
This file is part of ModeliMark.

ModeliMark is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

ModeliMark is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with ModeliMark.
If not, see <http://www.gnu.org/licenses/>.

Copyright 2011 Jens Frenkel
'''

import basemodelgenerator
import os

class AliasEqnClass(basemodelgenerator.BaseModelGenerator):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
    
    def generateModelSource(self, Number, dir):        

        assert(isinstance(Number,int))
        assert(isinstance(dir,str))

        modelname = 'modelalias_%s'%(Number)
        workdir = '%s/%s'%(dir,modelname)
        filename = '%s/%s.mo'%(workdir,modelname) 

        # new directory
        if not os.access(workdir, os.F_OK):
            os.mkdir(workdir)
        
        f = open( filename, 'w' )
        f.write('model %s\n'%(modelname))              

        f.write('  constant Integer N=%s;\n'%(str(Number)))
        f.write('  Real a[2*N+1];\n')

        f.write('equation\n')

        f.write('  der(a[1]) = N;\n')
        f.write('  a[2] = -a[1];\n')
        f.write('  a[3] = 2*a[2]+a[1];\n')

        f.write('  for i in 4:2:2*N loop\n')
        f.write('    a[i] = a[i-3] + a[i-2] - a[i-1];\n')
        f.write('    a[i+1] = i*a[i]+(i-1)*a[i-1];\n')
        f.write('  end for;\n')

        f.write('end %s;\n'%(modelname))
        f.close()        
        
        return modelname, [filename], workdir
                
